﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;


namespace Trade.DBUtility
{
    /// <summary>
    /// 登陆系统加解密服务
    /// </summary>
    public class EncryptDecryptService
    {
        /// <summary>
        /// MD5加密服务
        /// </summary>
        /// <param name="strEncrypt">密码明文</param>
        /// <returns>密码密文</returns>
        public static string MD5Encrypt(string strEncrypt)
        {
            StringBuilder strResult = new StringBuilder(1000);
            byte[] bEncrypt;
            MD5 md5 = new MD5CryptoServiceProvider();

            strEncrypt = strEncrypt == null ? string.Empty : strEncrypt;
            bEncrypt = System.Text.Encoding.UTF8.GetBytes(strEncrypt);
            byte[] bResult = md5.ComputeHash(bEncrypt);
            //十六进制方式输出
            foreach (byte b in bResult)
            {
                strResult.AppendFormat("{0:X2}", b);
            }
            return strResult.ToString();
        }
        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="strDESKey">DES密钥</param>
        /// <param name="strEncrypt">要加密的明文</param>
        /// <returns>加密后的密文</returns>
        public static string DESEncrypt(string strDESKey, string strEncrypt)
        {
            StringBuilder strResult = new StringBuilder(1000);
            //密钥
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();

            strEncrypt = strEncrypt == null ? string.Empty : strEncrypt;

            byte[] bEncrypt = System.Text.Encoding.UTF8.GetBytes(strEncrypt);

            des.Key = System.Text.Encoding.UTF8.GetBytes(strDESKey);
            des.IV = System.Text.Encoding.UTF8.GetBytes(strDESKey);

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

            cs.Write(bEncrypt, 0, bEncrypt.Length);
            cs.FlushFinalBlock();
            //十六进制方式输出
            foreach (byte b in ms.ToArray())
            {
                strResult.AppendFormat("{0:X2}", b);
            }
            return strResult.ToString();
        }
        /// <summary>
        /// DES解密
        /// </summary>
        /// <param name="strDESKey">DES密钥</param>
        /// <param name="strDecrypt">要解密的密文</param>
        /// <returns>解密后的明文</returns>
        public static string DESDecrypt(string strDESKey, string strDecrypt)
        {
            string strResult;
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            //把十六进制转换回字节  
            byte[] bDecrypt = new byte[strDecrypt.Length / 2];
            try
            {
                for (int i = 0; i < strDecrypt.Length / 2; i++)
                {
                    int iResult = Convert.ToInt32(strDecrypt.Substring(i * 2, 2), 16);
                    bDecrypt[i] = (byte)iResult;
                }
            }
            catch (Exception)
            {
                return string.Empty;
            }

            des.Key = System.Text.Encoding.UTF8.GetBytes(strDESKey);
            des.IV = System.Text.Encoding.UTF8.GetBytes(strDESKey);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

            cs.Write(bDecrypt, 0, bDecrypt.Length);
            try
            {
                cs.FlushFinalBlock();
            }
            catch (Exception)
            {
            }
            strResult = System.Text.Encoding.UTF8.GetString(ms.ToArray());
            return strResult;
        }
    }
}
